package cc.rengu.igas.bomp.common.dao.impl;

import cc.rengu.igas.bomp.common.dao.BompTCRoleAcctInfoMapper;
import cc.rengu.igas.bomp.common.entity.BompTCRoleAcctInfo;
import cc.rengu.jradp.mods.dbs.Database;
import cc.rengu.oltp.service.common.constant.AppConfigConstant;
import cc.rengu.oltp.service.common.constant.AppParamConstant;
import cc.rengu.oltp.utility.util.DbsUtil;
import cc.rengu.oltp.utility.util.XmlConfigUtil;
import cc.rengu.oltp.utility.util.XmlTreeUtil;
import cc.rengu.utility.cache.UnifiedCache;
import cc.rengu.utility.log.RgLog;
import cc.rengu.utility.log.RgLogger;

import java.util.List;
import java.util.stream.Collectors;

/**
 * 批量系统-角色账户信息表数据库操作接口实现
 *
 * @author Xuegui Cao
 * @date 2020/7/29
 * @description Copy By [momp-TCRoleAcctInfoMapperImpl]
 */
public class BompTCRoleAcctInfoMapperImpl implements BompTCRoleAcctInfoMapper {
    private RgLogger rglog = RgLog.getLogger(this.getClass().getName());
    String dbPoolName = XmlConfigUtil.getXmlConfigCacheKeyValue(AppParamConstant.DATABASE_CONF_NODE, AppConfigConstant.CSMC_POOL_NAME);

    /**
     * 使用主键查询营销活动法人机构对应营销户数据库操作接口实现
     *
     * @param instId       法人机构号
     * @param settleAcctId 科目/账号标识
     * @param roleId       角色标识
     * @return cc.rengu.igas.momp.common.entity.TCRoleAcctInfo 角色账户信息表
     * @throws Exception 异常信息
     * @author Jinan Liu
     * @date 2020/6/22 17:58
     */
    @Override
    public BompTCRoleAcctInfo selectInstitutionMcAccountByOrgIdAndAccountCategory(String instId, String settleAcctId, String roleId) throws Exception {

        BompTCRoleAcctInfo bompTCRoleAcctInfo = new BompTCRoleAcctInfo();
        bompTCRoleAcctInfo.setInstId(instId);
        bompTCRoleAcctInfo.setSettleAcctId(settleAcctId);
        bompTCRoleAcctInfo.setRoleId(roleId);

        XmlTreeUtil xmlTreeUtil = new XmlTreeUtil();
        String corporation = xmlTreeUtil.getXmlTreeStringValue(AppParamConstant.CORPORATION);
        if (null != corporation && !corporation.isEmpty()) {
            String dbPoolCashNode = corporation.trim() + AppParamConstant.DATABASE_CONF_NODE;
            dbPoolName = XmlConfigUtil.getXmlConfigCacheKeyValue(dbPoolCashNode, AppConfigConstant.CSMC_POOL_NAME);
        }
        rglog.error("DBPoolName=[{}]", dbPoolName);

        DbsUtil dbsUtil = new DbsUtil(dbPoolName);
        String sqlStr = (String) UnifiedCache.get(AppParamConstant.SQL_MAPPER_CACHE, "bompSelectInstitutionMcAccountByOrgIdAndAccountCategory");

        int iReturnCode = dbsUtil.dbsExecuteSql(sqlStr, bompTCRoleAcctInfo, bompTCRoleAcctInfo);
        if (Database.DBS_SUCCESS != iReturnCode) {
            rglog.error("查询法人机构号<{}>的机构<{}>的营销活动收款账号失败! RETURN_CODE=<{}>", instId, roleId, iReturnCode);
            return null;
        }

        return bompTCRoleAcctInfo;
    }

    /**
     * 使用主键查询营销活动法人机构对应营销户数据库操作接口实现
     *
     * @param instId       法人机构号
     * @param settleAcctId 科目/账号标识
     * @param roleId       角色标识
     * @return cc.rengu.igas.momp.common.entity.TCRoleAcctInfo 角色账户信息表
     * @throws Exception 异常信息
     * @author Jinan Liu
     * @date 2020/6/22 17:58
     */
    @Override
    public List<BompTCRoleAcctInfo> selectInstitutionMcAccountByOrgIdAndAccountCategoryList(String instId, String settleAcctId, String roleId) throws Exception {

        BompTCRoleAcctInfo tcRoleAcctInfo = new BompTCRoleAcctInfo();
        tcRoleAcctInfo.setInstId(instId);
        tcRoleAcctInfo.setSettleAcctId(settleAcctId);
        tcRoleAcctInfo.setRoleId(roleId);

        XmlTreeUtil xmlTreeUtil = new XmlTreeUtil();
        String corporation = xmlTreeUtil.getXmlTreeStringValue(AppParamConstant.CORPORATION);
        if (null != corporation && !corporation.isEmpty()) {
            String dbPoolCashNode = corporation.trim() + AppParamConstant.DATABASE_CONF_NODE;
            dbPoolName = XmlConfigUtil.getXmlConfigCacheKeyValue(dbPoolCashNode, AppConfigConstant.CSMC_POOL_NAME);
        }
        rglog.error("DBPoolName=[{}]", dbPoolName);

        DbsUtil dbsUtil = new DbsUtil(dbPoolName);
        String sqlStr = (String) UnifiedCache.get(AppParamConstant.SQL_MAPPER_CACHE, "bompSelectInstitutionMcAccountByOrgIdAndAccountCategory");

        List<Object> objectList = dbsUtil.dbsFetchLimitListData(sqlStr, tcRoleAcctInfo, BompTCRoleAcctInfo.class);
        if (null == objectList || objectList.isEmpty()) {
            rglog.error("查询法人机构号<{}>的机构<{}>的营销活动收款账号失败! ", instId, roleId);
            return null;
        }

        return objectList.stream().map(item -> (BompTCRoleAcctInfo) (item)).collect(Collectors.toList());
    }
}
